package com.lnking.lnkingplace.service.impl;

import com.lnking.lnkingplace.mapper.IsolationPlaceMapper;
import com.lnking.lnkingplace.mapper.IsolationRoomMapper;
import com.lnking.lnkingplace.model.entity.RoomSweepRecord;
import com.lnking.lnkingplace.mapper.RoomSweepRecordMapper;
import com.lnking.lnkingplace.model.enums.RoomType;
import com.lnking.lnkingplace.model.request.isolation.roomSweep.RoomSweepAddRequest;
import com.lnking.lnkingplace.model.request.isolation.roomSweep.RoomSweepUpdateRequest;
import com.lnking.lnkingplace.service.IRoomSweepRecordService;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-11-21
 */
@Service
public class RoomSweepRecordServiceImpl extends CustomServiceImpl<RoomSweepRecordMapper, RoomSweepRecord> implements IRoomSweepRecordService {

    @Resource
    IsolationPlaceMapper isolationPlaceMapper;
    @Resource
    IsolationRoomMapper isolationRoomMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createRoomSweep(RoomSweepAddRequest params) {
        RoomSweepRecord info = new RoomSweepRecord();
        BeanUtils.copyProperties(params,info);
        String placeId = isolationPlaceMapper.getPlaceId(params.getPlaceName());
        info.setPlaceId(placeId);
        info.setStartTime(LocalDateTime.now());
        this.baseMapper.insert(info);

        //开始消杀修改为消杀中
        isolationRoomMapper.updateStatus(placeId,params.getRoomNumber(), RoomType.DIRTYING_ROOM);

        return info.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void endRoomSweep(RoomSweepUpdateRequest params) {
        RoomSweepRecord info = new RoomSweepRecord();
        BeanUtils.copyProperties(params,info);
        info.setEndTime(LocalDateTime.now());
        this.baseMapper.insert(info);

        //结束消杀后改为可使用
        isolationRoomMapper.updateStatus(info.getPlaceId(),info.getRoomNumber(), RoomType.UNUSE);
    }
}
